/**
 * Vosao CMS. Simple CMS for Google App Engine.
 * Copyright (C) 2009 Vosao development team
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 * email: vosao.dev@gmail.com
 */

package org.vosao.plugins.csdata;

import org.vosao.business.mq.Topic;
import org.vosao.business.mq.message.SimpleMessage;
import org.vosao.business.plugin.AbstractPluginEntryPoint;
//import org.vosao.plugins.csdata.cronjob.CleanupConfirmationsJob;
import org.vosao.plugins.csdata.dao.CsdataDao;
import org.vosao.plugins.csdata.service.CsdataBackServiceManager;
import org.vosao.plugins.csdata.service.CsdataFrontServiceManager;
import org.vosao.plugins.csdata.servlet.CsdataServlet;
import org.vosao.service.plugin.PluginServiceManager;

public class CsdataEntryPoint extends AbstractPluginEntryPoint {

	private CsdataDao csdataDao;
	private CsdataBackServiceManager csdataBackSeriviceManager;
	private CsdataFrontServiceManager csdataFrontSeriviceManager;
	private CsdataVelocityPlugin velocityPlugin;
	
	@Override
	public void init() {
		getServlets().put("getdata", new CsdataServlet(
				getCsdataDao(),
				getCsdataFrontServiceManager().getCsdataFrontService()));
		//getJobs().add(new CleanupConfirmationsJob(
		//		getRegisterDao()));
	}
	@Override
	public void uninstall() {
		getBusiness().getMessageQueue().publish(new SimpleMessage(
				Topic.ENTITY_REMOVE, 
				getCsdataDao().getCsdataConfigDao().getKind()));
		getBusiness().getMessageQueue().publish(new SimpleMessage(
				Topic.ENTITY_REMOVE, 
				getCsdataDao().getCsdataUserDao().getKind()));
	}
	
	@Override
	public PluginServiceManager getPluginBackService() {
		return getCsdataBackServiceManager();
	}

	private CsdataBackServiceManager getCsdataBackServiceManager() {
		if (csdataBackSeriviceManager == null) {
			csdataBackSeriviceManager = new CsdataBackServiceManager(getBusiness(),
					getCsdataDao());
		}
		return csdataBackSeriviceManager;
	}
	private CsdataFrontServiceManager getCsdataFrontServiceManager() {
		if (csdataFrontSeriviceManager == null) {
			csdataFrontSeriviceManager = new CsdataFrontServiceManager(getBusiness(),
					getCsdataDao());
		}
		return csdataFrontSeriviceManager;
	}

	@Override
	public PluginServiceManager getPluginFrontService() {
		return getCsdataFrontServiceManager();
	}

	

	private CsdataDao getCsdataDao() {
		if (csdataDao == null) {
			csdataDao = new CsdataDao(getBusiness());
		}
		return csdataDao;
	}
	
	@Override
	public Object getPluginVelocityService() {
		if (velocityPlugin == null) {
			velocityPlugin = new CsdataVelocityPlugin(this, getBusiness(), 
					getCsdataDao());
		}
		return velocityPlugin;
	}
	
	@Override
	public String getBundleName() {
		return "org.vosao.plugins.csdata.resources.messages";
	}
}
